package com.jilijili.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jilijili.enums.MessageEnum;
import com.jilijili.mapper.repository.MsgRepository;
import com.jilijili.pojo.Users;
import com.jilijili.pojo.mo.MessageMo;
import com.jilijili.service.MsgService;
import com.jilijili.service.UsersService;
import com.jilijili.service.base.BaseInfoProperties;
import com.jilijili.utils.redis.RedisOperator;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @description
 * @createDate 2022-03-12 07:11:29
 */
@Service
public class MsgServiceImpl implements MsgService {

    //MongoDb的数据层
    @Resource
    private MsgRepository msgRepository;

    @Resource
    private RedisOperator redis;

    @Resource
    private UsersService usersService;


    /**
     * 删除消息
     *
     * @param fromUserId
     * @param toUserId
     */
    @Transactional
    @Override
    public void delMessageByFromUserIdAndToUsersId(String fromUserId, String toUserId) {
        this.msgRepository.deleteByFromUserIdAndToUserId(fromUserId, toUserId);
    }

    @Override
    public List<MessageMo> queryList(String toUserId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(
                page,
                pageSize,
                Sort.Direction.DESC,
                "createTime");
        List<MessageMo> messageMoList = this.msgRepository.findALLByToUserIdOrderByCreateTime(toUserId, pageRequest);
        for (MessageMo messageMo : messageMoList) {
            if (MessageEnum.F0LLOW_Y0U.type.equals(messageMo.getMsgType())) {
                Map map = messageMo.getMsgContent();
                if (messageMo.getMsgContent() == null) {
                    map = new HashMap();
                }
                String relationship = this.redis.get(
                        BaseInfoProperties.REDIS_FANS_AND_VLOGGER_RELATIONSHIP
                                + ":" + messageMo.getToUserId() + ":" + messageMo.getFromUserId());
                if (StringUtils.isNotBlank(relationship)) {
                    map.put("isFriend", true);
                } else {
                    map.put("isFriend", false);
                }
                messageMo.setMsgContent(map);
            }
        }
        return messageMoList;
    }


    /**
     * @author admin
     * @description 提供消息服务
     * @createDate 2022-03-12 07:11:29
     */
    @Override
    public void createMsg(String fromUserId,
                          String toUserId,
                          Integer type,
                          Map msgContent) {
        // TODO 查询用户
        Users user = usersService.getUser(fromUserId);

        // TODO 构建文档,设置字段
        MessageMo mo = MessageMo.builder().fromUserId(fromUserId)
                .fromNickname(user.getNickname())
                .fromFace(user.getFace())
                .toUserId(toUserId)
                .createTime(LocalDateTime.now()).build();
        if (type != null) {
            mo.setMsgType(type);
            mo.setMsgContent(msgContent);
        } else {
            mo.setMsgType(0);
        }

        // TODO 保存文档
        MessageMo mmo = msgRepository.save(mo);


    }
}




